﻿using ExcelToEntity.Helper;
using ExcelToEntity.Models;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ExcelToEntity
{
    /// <summary>
    /// 生成实体模板
    /// </summary>
    public static class EntityTemplate
    {
        private static string txtFilePath = Path.Combine(Directory.GetCurrentDirectory(), "EntityTemplate", "Entity.txt");
        private static StringBuilder sb = new StringBuilder();


        /// <summary>
        /// 添加注释
        /// </summary>
        /// <param name="desc"></param>
        private static void AddDesc(string desc)
        {
            sb.Append($"\r\n\t/// <summary>\r\n\t/// {desc}\r\n\t/// </summary>");
            return;
        }
        /// <summary>
        /// Description特性
        /// </summary>
        /// <param name="desc"></param>
        private static void AddDescriptionAttribute(string desc)
        {
            sb.Append($"\r\n\t[Description(\"{desc}\")]");
            return;
        }
        /// <summary>
        /// 创建实体
        /// </summary>
        /// <param name="rows"></param>
        /// <returns></returns>
        public static string CreateEntity(IEnumerable<ExcelModel> rows)
        {
            sb.Clear();
            foreach (var row in rows)
            {
                if (string.IsNullOrEmpty(row.DataName))
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(row.Description))
                {
                    AddDesc(row.Description);
                }
               
                if (!string.IsNullOrEmpty(row.DescriptionAttribute))
                {
                    AddDescriptionAttribute(row.DescriptionAttribute);
                }
                
                sb.Append($"\r\n\tpublic {row.DataType} {row.DataName} " + "{get;set;}");

            }
            return sb.ToString();
        }
        public static string CreateEntityClass(IEnumerable<ExcelModel> rows, string nameSpace, string className)
        {
            sb.Clear();
            StreamReader sr = new StreamReader(txtFilePath, Encoding.UTF8);
            var contentText = sr.ReadToEnd();
            sr.Close();
            foreach (var row in rows)
            {              
                if (string.IsNullOrEmpty(row.DataName))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(row.DataName))
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(row.Description))
                {
                    AddDesc(row.Description);
                }

                if (!string.IsNullOrEmpty(row.DescriptionAttribute))
                {
                    AddDescriptionAttribute(row.DescriptionAttribute);
                }
                if (!string.IsNullOrEmpty(row.DataType) && !string.IsNullOrEmpty(row.DataName))
                {
                    sb.Append($"\r\n\tpublic {row.DataType} {row.DataName} " + "{get;set;}");
                }
               
            }
            nameSpace = string.IsNullOrWhiteSpace(nameSpace) ? nameSpace = "mynamespace" : nameSpace;
            className = string.IsNullOrWhiteSpace(className) ? className = "MyClassName" : className;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("@nameSpace", nameSpace);
            dic.Add("@className", className);
            dic.Add("@templateProperties", sb.ToString());
            foreach (var item in dic)
            {
                contentText = contentText.Replace(item.Key, item.Value);
            }

            return contentText;
        }
        /// <summary>
        /// 属性名转拼音
        /// </summary>
        /// <param name="excelModels"></param>
        public static IEnumerable<ExcelModel> ConvertPinYin(IEnumerable<ExcelModel> excelModels)
        {
            List<ExcelModel> newExcelModels = new List<ExcelModel>();
            if (excelModels != null && excelModels.Any())
            {

                foreach (var item in excelModels)
                {
                    item.DataName = PinYinConverterHelp.ChinessToPingYinPascal(item.DataName);
                    newExcelModels.Add(item);
                }
            }
            return newExcelModels;
        }
    }
}
